package cn.smvc.task.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;

import cn.smvc.common.web.BaseServlet;
import cn.smvc.task.entity.TTimedTask;
import cn.smvc.task.service.TTimedTaskService;

@WebServlet("/timedtask")
public class TimedTaskServlet extends BaseServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 8634988552577151860L;
	
	@Autowired
	private TTimedTaskService tTimedTaskService;
	
	
	public void queryPage(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json; charset=utf-8");
		PrintWriter out = response.getWriter();
		
		
		Map<String, String[]> param = request.getParameterMap();
		Map<String, Object> result = tTimedTaskService.queryPage(param);
		
		out.println(JSON.toJSONString(result));
	}
	
	public void save(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException, SQLException {
		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json; charset=utf-8");
		PrintWriter out = response.getWriter();
		
		Map<String, String[]> param = request.getParameterMap();
		System.out.println(JSON.toJSONString(param));
		TTimedTask task = new TTimedTask();
		task.setId(param.get("id")[0]);
		task.setTaskName(param.get("taskName")[0]);
		task.setTaskClass(param.get("taskClass")[0]);
		task.setTaskGroup(param.get("taskGroup")[0]);
		task.setTaskCron(param.get("taskCron")[0]);
//		task.setRunFlag("F");
		task.setSwithFlag(param.get("swithFlag")[0]);
		int r = tTimedTaskService.save(task);
		Map<String, Object> result = new HashMap<String, Object>();
		if(r > 0) {
			result.put("succ", true);
			result.put("msg", "");
		} else {
			result.put("succ", false);
			result.put("msg", "");
		}
		out.println(JSON.toJSONString(result));
	}
	
	public void delete(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json; charset=utf-8");
		PrintWriter out = response.getWriter();
		
		String id = request.getParameter("id");
		int result=0;
		try {
			result = tTimedTaskService.delete(id );
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		out.println(JSON.toJSONString(result));
	}
	
	public void execute(HttpServletRequest request, HttpServletResponse response) {
		System.out.println("execute......");
		try {
			Thread.sleep(30000);
			request.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void queryAllTaskGroup(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException{
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json; charset=utf-8");
		PrintWriter out = response.getWriter();
		
		List<TTimedTask> result = tTimedTaskService.findAllTaskGroup();
		
		out.println(JSON.toJSONString(result));
	}
	
	public void findOne(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException{
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json; charset=utf-8");
		String id = request.getParameter("id");
//		System.out.println(id);
		TTimedTask task1 = tTimedTaskService.findOne(id);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("content", task1);
		
		result .put("success", true);
		result .put("msg", "查找成功！");
		PrintWriter out = response.getWriter();
		out.println(JSON.toJSONString(result));
		
	}
}